//
//  UserAccout.m
//  我的微博
//
//  Created by 陈尚飞 on 15/12/9.
//  Copyright © 2015年 tom-1. All rights reserved.
//

#import "UserAccout.h"

@implementation UserAccout
//一旦从服务器获得过期日期的事件,立即计算准确的日期
- (void)setExpires_in:(NSTimeInterval)expires_in {
    // 计算过期日期
    self.expiresDate = [NSDate dateWithTimeIntervalSinceNow:expires_in];
}
// kvc
- (instancetype)initWithDict:(NSDictionary *)dict {
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {}

// description
- (NSString *)description {
    NSArray *keys = @[@"access_token",@"expires_in",@"expires_in",@"uid",@"screen_name",@"avatar_large"];
    return [self dictionaryWithValuesForKeys:keys].description;
}

// 保存当前对象
- (void)saveUserAccount {
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    path = [path stringByAppendingPathComponent:@"account.plist"];
    NSLog(@"%@",path);
    // 归档保存
    [NSKeyedArchiver archiveRootObject:self toFile:path];
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.access_token forKey:@"access_token"];
    [aCoder encodeObject:@(self.expires_in) forKey:@"expires_in"];
    [aCoder encodeObject:self.uid forKey:@"uid"];
    [aCoder encodeObject:self.screen_name forKey:@"screen_name"];
    [aCoder encodeObject:self.avatar_large forKey:@"avatar_large"];
}

// 解档
- (void)initWithCoder:(NSCoder *)aDecoder {
    self.access_token = [aDecoder decodeObjectForKey:@"access_token"];
    self.expiresDate = [aDecoder decodeObjectForKey:@"expiresDate"];
    self.uid = [aDecoder decodeObjectForKey:@"uid"];
    self.screen_name = [aDecoder decodeObjectForKey:@"screen_name"];
    self.avatar_large = [aDecoder decodeObjectForKey:@"avatar_large"];
    
}
@end
